package com.bai.test;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * Created by Admin on 12/18 0018.
 */
public class JDBCTools {
    public static Connection getConnection() throws Exception{
        Properties properties=new Properties();
        InputStream  in=
                JDBCTools.class.getClassLoader().getResourceAsStream("jdbc.properties");

        properties.load(in);
        String driverClass=properties.getProperty("driverClass");
        String  url=properties.getProperty("url");
        String user=properties.getProperty("user");
        String password=properties.getProperty("password");
        properties.put("user", user);
        properties.put("password", password);

        Class.forName(driverClass);
        Connection connection= DriverManager.getConnection(url,properties);

        return connection;
    }

    public static void release(ResultSet resultSet,Statement statement,Connection connection){

        if(statement !=null){
            try{
                statement.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        if(connection !=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(resultSet !=null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}
